from flask import Flask, request, url_for,redirect
from jinja2.utils import generate_lorem_ipsum
app = Flask(__name__)

@app.route('/')
def index():
    name = request.args.get('name', 'Flask')
    return '<h1>Hello %s!</h1>' %name

@app.route('/goback/<int:year>')
def go_back(year):
    return f'<h1> Welcome to {2018-year}<h1>'

@app.route('/hello')
def hello():
    return f'<h1>hello</h1>'

@app.route('/foo')
def foo():
    page = url_for('do_something')
    return f'<h1> Foo Page<h1><a href={page}>do somthing</a>'

@app.route('/bar')
def bar():
    page = url_for('do_something')
    return f'<h1> Bar Page<h1><a href={page}>do somthing</a>'

@app.route('/do_something')
def do_something():
    return redirect(request.referrer or url_for('hello'))

@app.route('/more')
def load_post():
    return generate_lorem_ipsum(n=1)

@app.route('/post')
def show_post():
    post_body = generate_lorem_ipsum(n=2)
    return '''
<h1>A very long post</h1>
<div class="body">%s</div>
<button id="load">Load More</button>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
$(function(){
    $('#load').click(function() {
        $.ajax({
            url:'/more',
            type:'get',
            success:function(data){
                $('.body').append(data);
            }
        })
    })
})
</script>'''%post_body

